using System;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;

namespace OurWedding1.Models
{ 
    public class WeddingPhotoRepository : IWeddingPhotoRepository
    {
        readonly WeddingEntities _context = new WeddingEntities();

        public IQueryable<WeddingPhoto> All
        {
            get { return _context.WeddingPhotos; }
        }

        public IQueryable<WeddingPhoto> AllIncluding(params Expression<Func<WeddingPhoto, object>>[] includeProperties)
        {
            return includeProperties.Aggregate<Expression<Func<WeddingPhoto, object>>, IQueryable<WeddingPhoto>>(_context.WeddingPhotos, (current, includeProperty) => current.Include(includeProperty));
        }

        public WeddingPhoto Find(int id)
        {
            return _context.WeddingPhotos.Find(id);
        }

        public void InsertOrUpdate(WeddingPhoto weddingphoto)
        {
            if (weddingphoto.WeddingPhotoId == default(int)) {
                // New entity
                _context.WeddingPhotos.Add(weddingphoto);
            } else {
                // Existing entity
                _context.Entry(weddingphoto).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var weddingphoto = _context.WeddingPhotos.Find(id);
            _context.WeddingPhotos.Remove(weddingphoto);
        }

        public void Save()
        {
            _context.SaveChanges();
        }

        public void Dispose() 
        {
            _context.Dispose();
        }
    }

    public interface IWeddingPhotoRepository : IDisposable
    {
        IQueryable<WeddingPhoto> All { get; }
        IQueryable<WeddingPhoto> AllIncluding(params Expression<Func<WeddingPhoto, object>>[] includeProperties);
        WeddingPhoto Find(int id);
        void InsertOrUpdate(WeddingPhoto weddingphoto);
        void Delete(int id);
        void Save();
    }
}